<?php
namespace Admin\Controller;
use Think\Controller;

class SystemController extends \Org\Util\AdminController {



    /** 
     * 权限验证标志位,默认true
     * true:所有action都需要权限验证;
     * false:所有action都不需要权限验证;
     * array:array中的action不需要权限验证，其余需要。
     */
    protected $authority = true;
    

    /**
     * 创建之后做的事情
     */
    public function interfaceAfterCreate($id) {
    }

    /**
     * 删除之前做的事情
     */
    public function interfaceBeforeDelete($id) {
    }

    /**
     * 更新之前做的事情
     */
    public function interfaceBeforeUpdate($id) {
    }

    /**
     * 父类中已经有典型的CRUD操作并预留入口
     * 如有较大改动则覆盖一下
     */
    public function create() {
        parent::create();
    }

    public function delete() {
        parent::delete();
    }

    public function update($id) {
        $model = D(CONTROLLER_NAME);
        if (IS_POST) {
            $_POST['id'] = $id;
            if (count($_FILES) !== 0) {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 99999999;// 设置附件上传大小
                $upload->rootPath = './Public/file/'; // 设置附件上传根目录
                $upload->savePath = ''; // 设置附件上传（子）目录
                $upload->autoSub = false;
                $info = $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                } else {
                    foreach ($_FILES as $field=>$file) {
                        $_POST[$field] = $info[$field]['savename'];
                        if ($info[$field]['savename']) {
                            if (isset($id)) {
                                $modelTemp = $model->find($id);
                                unlink('./Public/file/'.$modelTemp[$field]);
                            }
                        }
                    }
                }
            }
            if (!$model->create()) {
                $this->error($model->getError());
                return false;
            }
            $model->save();
            $this->success('修改成功!', U('index'));
        }
        $modelData = $model->getById($id);
        if (!$modelData) {
            $this->error('非法操作!');
            return false;
        }
        $this->assign('model', $modelData);
        $this->display();
    }

    public function list() {
        parent::list();
    }

    public function index() {
        $this->display();
    }

    public function game($id) {
        $model = D(CONTROLLER_NAME);
        if (IS_POST) {
            $sum = 0;
            foreach ($_POST['name'] as $k => $v) {
                if (empty($v)) {
                    unset($_POST['name'][$k]);
                    unset($_POST['number'][$k]);
                    unset($_POST['prob'][$k]);
                    unset($_POST['user'][$k]);
                }
            }
            foreach ($_POST['prob'] as $v) {
                $sum += $v;
            }
            if ($sum != 100) {
                $this->error('概率之和不为100！');
            }
            if (count($_FILES) !== 0) {
                $upload = new \Think\Upload();
                $upload->maxSize = 0; 
                $upload->rootPath = './Public/file/';
                $upload->savePath = '';
                $upload->autoSub = false;
                $info = $upload->upload();
                if ($info) {
                    //上传成功，删除原有文件
                    foreach ($_FILES as $field => $file) {
                        $_POST[$field] = $info[$field]['savename'];
                        if ($info[$field]['savename']) {
                            if (isset($id)) {
                                $modelTemp = $model->find($id);
                                unlink('./Public/file/' . $modelTemp[$field]);
                            }
                        }
                    }
                    if (!empty($_POST['pclogo'])) {
                        $model->pclogo = $_POST['pclogo'];
                    }
                    if (!empty($_POST['pcpicture'])) {
                        $model->pcpicture = $_POST['pcpicture'];
                    }
                    if (!empty($_POST['pcpicture1'])) {
                        $model->pcpicture1 = $_POST['pcpicture1'];
                    }
                    if (!empty($_POST['pcpicture2'])) {
                        $model->pcpicture2 = $_POST['pcpicture2'];
                    }
                    if (!empty($_POST['pcpicture3'])) {
                        $model->pcpicture3 = $_POST['pcpicture3'];
                    }
                }
            }
            $model->likes_num = $_POST['likes_num'];
            $model->alertdetail = $_POST['alertdetail'];
            $model->votes_num = $_POST['votes_num'];
            $model->prize_url = $_POST['prize_url'];
            $model->prize_url = $_POST['prize_url'];
            $model->pcpictureurl = $_POST['pcpictureurl'];
            $model->votedetail = $_POST['votedetail'];
            $model->prize_count = intval($_POST['prize_count']);
            $model->prize_name = json_encode($_POST['name']);
            $model->prize_number = json_encode($_POST['number']);
            $model->prize_prob = json_encode($_POST['prob']);
            $model->prize_user = json_encode($_POST['user']);
            $model->where(['id' => $id])->save();
            $this->success('修改成功!', U('game', array('id' => 1)));
        }
        $modelData = $model->getById($id);
        if (!$modelData) {
            $this->error('非法操作!');
            return false;
        }
        
        
        $modelData['prize_name'] = json_decode($modelData['prize_name']);
        $modelData['prize_number'] = json_decode($modelData['prize_number']);
        $modelData['prize_prob'] = json_decode($modelData['prize_prob']);
        $modelData['prize_user'] = json_decode($modelData['prize_user']);
        $this->assign('model', $modelData);
        $this->display();
    }

    public function gamelist() {
		$searchMap = $this->handleSearchMap($_GET["searchMap"]);
        if ($_GET['searchMap']['prize'] === '0') {
            $searchMap['prize'] = 0;
        }
		$model = D('travelprize');
		$count = $model->where($searchMap)->count();
		$page = new \Think\AdminPage($count, C('PAGE_NUM'));
		$show = $page->show();
		$list = $model->where($searchMap)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $system = D('System')->where('id = 1')->find();
        $prize = json_decode($system['prize_name'], true);
        foreach ($list as &$val) {
            $user = D('user')->where(['id' => $val['userid']])->find();
            $val['username'] = $user['username'];
            $val['prize'] = $prize[$val['prize']];

        }
		$this->assign('prizelist', $prize);
		$this->assign('list', $list);
		$this->assign('page', $show);
        $this->display('gamelist');
    }

    public function gamedelete() {
        $id = $_POST['id'];
        D('travelprize')->delete($id);
        $ret = array(
            'status' => 1,
            'msg' => '删除成功'
        );
        $this->ajaxReturn($ret, 'json');
    }
}
